function vCharge = Trigen(lambda, NOS, vRef, v_osp, v_osn)
       
%
% User: armin jalili
% Project name: stuck
% Project area: /proj/es/shekes/stuck
%
% Department of Electrical Engineering
% Linkoping University 
%
% Fri Jul  2 09:27:18 CEST 2010
%
% Modified by jjwikner

    if (nargin < 3)
        v_osp = 0;
        v_osn = 0;
    end;
    
    if (nargin < 2)        
        NOS = 2^14;
    end

    if (nargin < 1)
        lambda = 0.01;
    end;

    % Some default values
    I_ref = 55e-6;
    C    = 2e-12;
    V_dd      = 1.8;

    % V_refn    = ( V_dd / 2.0 ) -vRef/2.0 ;
    % V_refp    = V_refn + vRef;
    
    % Normalize to simplify calculations...
    V_refn    = -vRef/2.0;
    V_refp    = +vRef/2.0;
    V_mid     = (V_refp + V_refn)/2.0;    
    kappa     = I_ref / C / ( 1 + lambda * V_mid);
    
    % 
    
    t = linspace(0, (2*vRef+v_osp+v_osn)/kappa/(1+lambda), NOS);
    
    max(t);
    
   
    charge    =   kappa * t * (1 + 0*lambda*V_dd)  + ...
        (V_refn+v_osn)*exp(-lambda*kappa*t);
    max(charge)+V_dd/2.0;
    min(charge)+V_dd/2.0;
    
    discharge =  -kappa * t + (V_refp+v_osp)*exp(-lambda*kappa*t);
    max(discharge)+V_dd/2.0;
    min(discharge)+V_dd/2.0;

    vCharge    =  [ charge(find(charge < (V_refp + v_osp))) ...
                    discharge(find(discharge > (V_refn + v_osn)))];
    
    %    vCharge    = V_dd /2.0 + vCharge;
    
    % plot(vCharge)
